# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
# 添加Django自带的分页插件 paginator
from django.core.paginator import Paginator
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger

from .models import *

# Create your views here.
def index(request):
    limit = 10  # 每页显示的记录数
    worker_list = Worker_info.objects.all().order_by('-worker_apply_date')
    paginator = Paginator(worker_list, limit)  # 实例化一个分页对象
    page = request.GET.get('page')  # 获取页码
    try:
        worker_list = paginator.page(page)  # 获取某页对应的记录
    except PageNotAnInteger:  # 如果页码不是个整数
        worker_list = paginator.page(1)  # 取第一页的记录
    except EmptyPage:  # 如果页码太大，没有相应的记录
        worker_list = paginator.page(paginator.num_pages)  # 取最后一页的记录
    return render(request, 'pep/list.html', context={'worker_list': worker_list})